package com.utf999.qiangmanger.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.utf999.qiangmanger.controller.permission.AuthorityAnnotation;
import com.utf999.qiangmanger.controller.permission.AuthorityScope;
import com.utf999.qiangmanger.controller.permission.PromptModeType;
import com.utf999.qiangmanger.entity.JsonEntity;
import com.utf999.qiangmanger.entity.PageEntity;
import com.utf999.qiangmanger.exception.BizException;
import com.utf999.qiangmanger.exception.ResponseCode;
import com.utf999.qiangmanger.service.iface.IOrderInfoService;

/**
 * 报表MVC控制器
 * @author yinwenjie
 */
@Controller
@RequestMapping(value={"/report"})
public class ReportController extends BaseController {
	
	/**
	 * 日志记录
	 */
	private static Log controllerLog4j = LogFactory.getLog(ReportController.class);

	/**
	 * 
	 */
	@Autowired
	private IOrderInfoService orderInfoService;
	
	/**
	 * 进行公司级报表的数据准备页面
	 * @param request
	 * @param response
	 * @return 
	 */
	@RequestMapping(value={"/perReportOrderInfoByCompany"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.REDIRECT)
	public ModelAndView perReportOrderInfoByCompany(HttpServletRequest request , HttpServletResponse response) {
		return new ModelAndView("workorder/companyOrderReport");
	}
	
	/**
	 * 进行店面级报表的数据准备页面
	 * @param request
	 * @param response
	 * @return 
	 */
	@RequestMapping(value={"/perReportOrderInfoByStorefront"})
	@AuthorityAnnotation(scope={AuthorityScope.StoreAdmin , AuthorityScope.CompanyAdmin , AuthorityScope.SuperAdmin} , promptMode=PromptModeType.REDIRECT)
	public ModelAndView perReportOrderInfoByStorefront(HttpServletRequest request , HttpServletResponse response) {
		return new ModelAndView("workorder/storeOrderReport");
	}
	
	/**
	 * 以公司为单位进行工单报表的输出
	 * @param request 
	 * @param response 
	 * @param companyid 必须传入的公司编号信息
	 * @param orderMode 可能作为查询条件的派单方式
	 * @param workNumber 可能作为查询条件的服务员工号
	 * @param roomNo 可能作为查询条件的房间号
	 * @param beginFilingTime 可能指定的归档时间的开始点范围
	 * @param endFilingTime 可能指定的归档时间的结束点范围
	 * @param pageNum 分页页码，从0开始
	 * @param maxrow 每页的最大记录数量，如果没有传入则会默认为20
	 */
	@RequestMapping(value={"/reportOrderInfoByCompany"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void reportOrderInfoByCompany(HttpServletRequest request , HttpServletResponse response , 
			String companyid, String workNumber, String beginFilingTime,
			String endFilingTime, Integer pageNum, Integer maxrow) {
		JsonEntity jsonEntity = new JsonEntity();
		
		PageEntity pageEntity = null;
		try {
			pageEntity = this.orderInfoService.reportOrderInfo(companyid, null, null, workNumber, null, beginFilingTime, endFilingTime, pageNum, maxrow);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(pageEntity);

			this.writeResponseMsg(response, jsonEntity , new String[]{"accountAdmin","storefront", "company" , "defines" , "groups", "userGroups" , "orderInfo" , "commodityType" , "uniqueKey" , "encodePassword" , "commoditys"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 以店面为单位进行工单报表的输出
	 * @param request
	 * @param response
	 * @param storefrontid 必须传入的店面编号信息
	 * @param orderMode 可能作为查询条件的派单方式
	 * @param workNumber 可能作为查询条件的服务员工号
	 * @param roomNo 可能作为查询条件的房间号
	 * @param beginFilingTime 可能指定的归档时间的开始点范围
	 * @param endFilingTime 可能指定的归档时间的结束点范围
	 * @param pageNum 分页页码，从0开始
	 * @param maxrow 每页的最大记录数量，如果没有传入则会默认为20
	 */
	@RequestMapping(value={"/reportOrderInfoByStorefront"})
	@AuthorityAnnotation(scope={AuthorityScope.StoreAdmin , AuthorityScope.CompanyAdmin, AuthorityScope.SuperAdmin} , promptMode=PromptModeType.AJAX)
	public void reportOrderInfoByStorefront(HttpServletRequest request , HttpServletResponse response , 
			String storefrontid, Integer orderMode, String workNumber, String roomNo,
			String beginFilingTime, String endFilingTime, Integer pageNum, Integer maxrow) {
		JsonEntity jsonEntity = new JsonEntity();
		
		PageEntity pageEntity = null;
		try {
			pageEntity = this.orderInfoService.reportOrderInfo(null, storefrontid, orderMode, workNumber, roomNo, beginFilingTime, endFilingTime, pageNum, maxrow);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(pageEntity);

			this.writeResponseMsg(response, jsonEntity , new String[]{"accountAdmin","storefront", "company" , "defines" , "groups", "userGroups" , "orderInfo" , "commodityType" , "uniqueKey" , "encodePassword" , "commoditys"});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 以人员业绩为单位进行工单报表的输出
	 * @param request 
	 * @param response 
	 * @param companyid 必须传入的公司编号信息
	 * @param orderMode 可能作为查询条件的派单方式
	 * @param workNumber 可能作为查询条件的服务员工号
	 * @param roomNo 可能作为查询条件的房间号
	 * @param beginFilingTime 可能指定的归档时间的开始点范围
	 * @param endFilingTime 可能指定的归档时间的结束点范围
	 * @param pageNum 分页页码，从0开始
	 * @param maxrow 每页的最大记录数量，如果没有传入则会默认为20
	 */
	@RequestMapping(value={"/reportTotalUserGroupByCompany"})
	@AuthorityAnnotation(scope={AuthorityScope.SuperAdmin , AuthorityScope.CompanyAdmin} , promptMode=PromptModeType.AJAX)
	public void reportTotalUserGroupByCompany(HttpServletRequest request , HttpServletResponse response ,
			String companyid, String storefrontid, Integer orderMode, String workNumber,
			String roomNo, String beginFilingTime, String endFilingTime,
			Integer pageNum, Integer maxrow) {
		JsonEntity jsonEntity = new JsonEntity();
		
		PageEntity pageEntity = null;
		try {
			pageEntity = this.orderInfoService.reportTotalGroupbyUserid(companyid, null, orderMode, workNumber, roomNo, beginFilingTime, endFilingTime, pageNum, maxrow);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(pageEntity);

			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
	
	/**
	 * 以店面为单位进行人员业绩报表的输出
	 * @param request
	 * @param response
	 * @param storefrontid 必须传入的店面编号信息
	 * @param orderMode 可能作为查询条件的派单方式
	 * @param workNumber 可能作为查询条件的服务员工号
	 * @param roomNo 可能作为查询条件的房间号
	 * @param beginFilingTime 可能指定的归档时间的开始点范围
	 * @param endFilingTime 可能指定的归档时间的结束点范围
	 * @param pageNum 分页页码，从0开始
	 * @param maxrow 每页的最大记录数量，如果没有传入则会默认为20
	 */
	@RequestMapping(value={"/reportTotalUserGroupByStorefront"})
	@AuthorityAnnotation(scope={AuthorityScope.StoreAdmin , AuthorityScope.CompanyAdmin, AuthorityScope.SuperAdmin} , promptMode=PromptModeType.AJAX)
	public void reportTotalUserGroupByStorefront(HttpServletRequest request , HttpServletResponse response ,
			String companyid, String storefrontid, Integer orderMode, String workNumber,
			String roomNo, String beginFilingTime, String endFilingTime,
			Integer pageNum, Integer maxrow) {
		JsonEntity jsonEntity = new JsonEntity();
		
		PageEntity pageEntity = null;
		try {
			pageEntity = this.orderInfoService.reportTotalGroupbyUserid(null, storefrontid, orderMode, workNumber, roomNo, beginFilingTime, endFilingTime, pageNum, maxrow);
			jsonEntity.setResponsecode(ResponseCode.E200);
			jsonEntity.setErrorinfo("");
			jsonEntity.setData(pageEntity);

			this.writeResponseMsg(response, jsonEntity , new String[]{""});
		} catch (BizException e) {
			controllerLog4j.error(e.getMessage(), e);
			jsonEntity.setResponsecode(e.getResponseCode());
			jsonEntity.setData("");
			jsonEntity.setErrorinfo(e.getMessage());
			this.writeResponseMsg(response, jsonEntity);
		}
	}
}
